//
//  AllOrderTypeViewController.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/26.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "AllOrderTypeViewController.h"
#import "AllOrderViewModel.h"
#import "FillInTheOrderViewController.h"
#import "PCPayViewController.h"
#import "OrderInformationViewController.h"
#import "DownloadContractViewController.h"
#import "ApplyInvoiceViewController.h"
#import "ShopCarViewController.h"
#import "AllOrderModel.h"
#import "GLGWPopTosView.h"
#import <UMShare/UMShare.h>

@interface AllOrderTypeViewController ()<OrderBottomOperationDelegate>
{
    NSMutableDictionary *requestDic;
}
@property (nonatomic,strong) AllOrderViewModel * allOViewModel;
@property (nonatomic, strong) AllOrderModel *model;
@end

@implementation AllOrderTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allOViewModel = [[AllOrderViewModel alloc] initWithViewToViewModel:self.groupedTableView theSuperView:self.view];
    self.allOViewModel.type = self.type;
    self.allOViewModel.delegate = self;
    kWeakSelf(self);
    self.allOViewModel.BlockBttomBtnClick = ^(GLGWButton * _Nonnull btn, AllOrderDetailListModel * _Nonnull listModel) {
        [weakself bottomBtnClick:btn orderModel:listModel];
    };
    requestDic = [NSMutableDictionary dictionary];
    //            LoginModel *modles = [NSKeyedUnarchiver unarchiveObjectWithData:self.userInforData];
    //            param[@"userId"] = modles.data.userId;
    //            @"statusFlag" : koString([NSString stringWithFormat:@"%ld",self.type]) // 0:全部 1：确认中2：待支付；3：线下支付待确认；4：待商家发货；5：待收货；6：交易完成
    requestDic[@"statusFlag"]  = koString(@"2");
    requestDic[@"userId"] = koString(@"1231232");
    requestDic[@"pageSize"] = @(20);
    [self headerRereshing];
}

- (void)bottomBtnClick:(GLGWButton *)btn orderModel:(AllOrderDetailListModel *)orderModel
{
    switch (btn.tag) {
        case 340: { // 下载合同
            DownloadContractViewController *vc = [DownloadContractViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 341: {// 再次购买
            ShopCarViewController *vc = [ShopCarViewController new];
            NSArray *arr = orderModel.orderGoods;
            NSMutableString *muStr = [NSMutableString string];
            for (int i = 0; i < arr.count; i ++) {
                AllOrderDetailListObjectModel *listObjectModel = arr[i];
                if (i == arr.count - 1) {
                    [muStr appendFormat:@"%@",listObjectModel.goodsId];
                } else {
                    [muStr appendFormat:@"%@,",listObjectModel.goodsId];
                }
            }
            vc.goodsIDStr = muStr;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 342: // 取消并撤回
            
            break;
        case 343: // 取消
            
            break;
        case 344: // 去支付
            [self fillInTheOrder:orderModel.orderId];
            break;
        case 345: // 确认收货
            
            break;
        case 346: { // 提货单
            DownloadContractViewController *vc = [DownloadContractViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 347: {// 申请发票
            ApplyInvoiceViewController *vc = [ApplyInvoiceViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 348: // 更多按钮
//            [self showMoreAlertV:btn];
            break;
        case 349: // 分享
            [self shareWebPageToPlatformType:UMSocialPlatformType_WechatSession model:orderModel];
            break;
        default:
            break;
    }
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType model:(AllOrderDetailListModel *)model
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"分享标题" descr:@"分享内容描述" thumImage:[UIImage imageNamed:@"icon"]];
    //设置网页地址
    shareObject.webpageUrl = @"https://baidu.com";
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        } else {
//            NSLog(@"response data is %@",data);
            GLGWPopTosView *p_View = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Success];
            p_View.tosTitle = @"发送成功!";
        }
    }];
}

#pragma mark -- OrderBottomOperationDelegate
- (void)fillInTheOrder:(NSString *)orderID
{
    FillInTheOrderViewController * fillTheVC = [FillInTheOrderViewController new];
    fillTheVC.orderID = orderID;
    [self.navigationController pushViewController:fillTheVC animated:YES];
}

- (void)payTheOrder
{
    PCPayViewController * payVC = [PCPayViewController new];
    [self.navigationController pushViewController:payVC animated:YES];
}

- (void)orderListDidSelectRowAtIndexPath:(AllOrderDetailListModel *)orderListModel
{
    OrderInformationViewController *orderInforVc = [OrderInformationViewController new];
    orderInforVc.orderID = orderListModel.orderId;
    [self.navigationController pushViewController:orderInforVc animated:YES];
}

- (void)headerRereshing
{
    self.page = 1;
    [self loadData];
}

- (void)footerRereshing
{
    if (self.page < self.model.data.total) {
        self.page ++;
        [self loadData];
    } else {
        [GLGWProgressHUD showNotice:@"没有更多服务的买家了！" view:nil];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }
}

- (void)loadData
{
    requestDic[@"pageNo"] = @(self.page);
    @weakify(self)
    [[self.allOViewModel.invoiceCommand execute:requestDic] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        self.model = (AllOrderModel *)x;
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
}

@end
